﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace Company.StudyClub.BLL.Cache
{
    public class CacheManager
    {
        private static readonly System.Web.Caching.Cache _cache;
        static CacheManager()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                _cache = context.Cache;
            }
            else
            {
                _cache = HttpRuntime.Cache;
            }
        }

        public const string LANGUAGE_KEY = "LanguageLayer";
        public const int LANGUAGE_REFRESH_LENGTH = 10;

        public const string CONFIG_KEY = "ConfigLayer";
        public const int CONFIG_REFRESH_LENGTH = 30;

        public const string AREA_MAP_KEY = "AreaLayer";
        public const int AREA_MAP_REFRESH_LENGTH = 60;

        public const string DOMAIN_SETTING_KEY = "DomainSettingLayer";
        public const int DOMAIN_SETTING_REFRESH_LENGTH = 30;

        public const string REGION_KEY = "RegionLayer";
        public const int REGION_KEY_LENGTH = 60;

        public const int DEFAULT_EXPIRED_LENGTH = 20;

        /// <summary>
        /// 插入一个数据到Catche 
        /// </summary>
        /// <param name="source">Objcet类型</param>
        /// <param name="keyName"> Key</param>
        /// <param name="ts">一个间隔时间</param>
        public static void Insert(object source, string keyName, TimeSpan ts)
        {
            if (ts.TotalMinutes == 0)
            {
                _cache.Insert(keyName, source, null, DateTime.MaxValue, TimeSpan.Zero);
            }
            else
            {
                _cache.Insert(keyName, source, null, DateTime.Now.AddMinutes(ts.TotalMinutes), TimeSpan.Zero);
            }
        }
        public static void Insert(object source, string keyName)
        {
            Insert(source, keyName, TimeSpan.FromMinutes(DEFAULT_EXPIRED_LENGTH));
        }

        public static object Get(string keyName)
        {
            return _cache.Get(keyName);
        }
    }
}
